package cc.linker.steplink.crm.merchanth5.service;


import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchanth5.dao.ZsGoodsDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ZsGoodsService {

    @Autowired
    private ZsGoodsDao zsGoodsDao;

    public List getList(String orgId) {
        List<Map<String, Object>> goodsList = zsGoodsDao.getGoodsList(orgId);
        if (null == goodsList || goodsList.size() == 0) {
            return goodsList;
        }
        for (Map<String, Object> go : goodsList) {
            go.put("title", "");
            go.put("cover", "");
            Map<String, Object> goodsInfo;
            if (3 == (int) go.get("goodsType")) {
                //知识付费（内容）
                go.put("moneyAmount", "在专栏中购买");
                goodsInfo = zsGoodsDao.getGoodsContent((Long) go.get("goodsId"));
            } else {
                //知识付费（专栏）
                go.put("moneyAmount", NumberUtils.fenToYuan((Long) go.get("moneyAmount")));
                goodsInfo = zsGoodsDao.getGoodsContent((Long) go.get("goodsId"));
            }
            if (null != goodsInfo) {
                go.put("title", goodsInfo.get("title"));
                go.put("cover", goodsInfo.get("cover"));
            }
        }
        return goodsList;
    }

    /**
     * 保存商品分享记录
     * @param goodsId
     * @param shareCustomerId
     * @param shareType
     * @param openCustomerId
     */
    public void setGoodsShareLog(Long goodsId,String shareCustomerId,Integer shareType,String openCustomerId){
        try{
            if(goodsId!=null && !StringUtils.isEmpty(shareCustomerId) && !StringUtils.isEmpty(openCustomerId) && shareType!=null){
                if(!shareCustomerId.equals(openCustomerId)){
                    zsGoodsDao.setGoodsShareLog(goodsId,shareCustomerId,shareType,openCustomerId);
                }
            }else{
                LOGGER.info("保存商品分享记录错误：goodsId："+goodsId+",shareCustomerId:"+shareCustomerId+",shareType:"+shareType+",openCustomerId:"+openCustomerId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
